/**
 * 
 */
package com.ms.game.offlineSpin.async;

import com.ms.game.offlineSpin.OfflineSpinResultData;
import com.ms.game.offlineSpin.OfflineSpinStopCondition;
import com.ms.game.offlineSpin.message.SOfflineSpinResultGetResult;
import com.xpec.c4.db.dao.PlayerDAO;
import com.xpec.c4.game.time.TimeSys;
import com.xpec.c4.game.utility.GUID;
import com.xpec.c4.message.MessageTypes;
import com.xpec.c4.service.message.MessageAction;
import com.xpec.c4.service.module.task.TaskAsyncCommand;

/**
 * @author TW15010
 *
 */
public class LoadOfflineSpinResultAsync extends TaskAsyncCommand {
	/**
	 * player guid
	 */
	private GUID playerId;

	/**
	 * client 的 sssion id
	 */
	private GUID sessionId;

	/**
	 * 離線 spin 結果
	 */
	private OfflineSpinResultData result;

	@Override
	public void asyncExecute() throws Exception {
		result = PlayerDAO.loadOfflineSpinResult(playerId);
		
		if(result != null){
			//如果登入來拿的時候還沒有寫過停止狀況
			if(result.getStopReason() == 0 && result.getStopTime() == 0L){
				//就告訴client是因為登入才停止
				//因為玩家一登入時，離線SPIN的INFO就會被切到STANDBY(不會再繼續SPIN)
				result.setStopReason(OfflineSpinStopCondition.LOGIN.getStringTableID());
				result.setStopTime(TimeSys.currentTimeSec());
			}
		}
		
		sendResultToClient();
	}

	@Override
	public void execute() throws Exception {
	}

	private void sendResultToClient() {
		if (sessionId == null) {
			return;
		}

		// 將結果回傳給 client
		SOfflineSpinResultGetResult msg = new SOfflineSpinResultGetResult();
		msg.setResult(result);

		MessageAction.sendMessage(sessionId, MessageTypes.S_OFFLINE_SPIN_RESULT_GET_RESULT, msg);
	}

	public GUID getSessionId() {
		return sessionId;
	}

	public void setSessionId(GUID sessionId) {
		this.sessionId = sessionId;
	}

	public OfflineSpinResultData getResult() {
		return result;
	}

	public void setResult(OfflineSpinResultData result) {
		this.result = result;
	}

	public GUID getPlayerId() {
		return playerId;
	}

	public void setPlayerId(GUID playerId) {
		this.playerId = playerId;
	}
}
